package com.gainloop.Utils;

import java.util.ArrayList;
import java.util.LinkedList;

import org.alexd.jsonrpc.JSONRPCException;

import com.gainloop.Api.Tags;
import com.gainloop.Api.Entity.Category;
import com.gainloop.Api.Entity.Tag;

/**
 * A class for converting between entity to string and vice versa.
 * @author Meir
 *
 */
public class ConvertUtils {
	
	
	/**
	 * This method will transform an ArrayList of Category, to their string representation. 
	 * @param allCategories - the input to convert.
	 * @return a string array of the Categories names.
	 */
	public static String[] transformToStringArray(ArrayList<Category> allCategories) {
    	String[] newStrArr = new String[allCategories.size()];
    	int i=0;
    	for(Category curCategory:allCategories){
    		newStrArr[i]=curCategory.getName();
    		i++;
    	}
		return newStrArr;
	}
	
	/**
	 * This will transform the list of tags from string to Tag entities.
	 * @param _globalTagList2 - the the list of the tags names.
	 * @return a list of Tags objects.
	 */
	public static ArrayList<Tag> transformToTagList(LinkedList<String> _globalTagList2) {
		ArrayList<Tag> returnTagList = new ArrayList<Tag>();
		Tags tags = new Tags();
		Tag newTag;
		for(String curTagStr:_globalTagList2){
			try {
				newTag = tags.getByName(curTagStr);
				returnTagList.add(newTag);
			} catch (JSONRPCException e) {
				e.printStackTrace();
				throw new RuntimeException("Failed to create a Tag");
			}
		}
		return returnTagList;
	}
	
	
}
